package classfile
import (
	. "jvm/typedef"
)
type ConstantPool []ConstantInfo

//public
func newConstantPool(reader *BytecodeReader) (constantPool ConstantPool){
	constantPoolCount := reader.readU2()
	constantPool = make([]ConstantInfo, constantPoolCount)
	for i := 1; i < int(constantPoolCount); i++ {
		constantPool[i] = newConstantInfo(reader, constantPool)
		constantPool[i].readInfo(reader)
		//如果是Long或Double i++
		switch constantPool[i].(type) {
		case *ConstantLong, *ConstantDouble:
			i++
		}
	}
	return
}
func (self *ConstantPool) GetUtf8(index U2) string {
	return self.getUtf8(index)
}
func (self *ConstantPool) GetConstantInfo(index U2) ConstantInfo {
	return self.getConstantInfo(index)

}

//***********************************************************
//private
func (self *ConstantPool) getUtf8(index U2) string {
	utf8, ok := (*self)[index].(*ConstantUtf8)
	if !ok {
		panic("常量池下标错误 classfile.constant_pool.go.getUtf8")
	}
	return utf8.val
}
func (self *ConstantPool) getConstantInfo(index U2) ConstantInfo {
	info := (*self)[index]
	return info

}

//***********************************************************
//getter


//***********************************************************